//
//  AppDelegate.m
//  EducationOID
//
//  Created by slz on 2017/5/8.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "AppDelegate.h"
#import "OIWindow.h"
#import "DataCenter.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

+ (AppDelegate*)sharedInstance;
{
    return (id)[UIApplication sharedApplication].delegate;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [[DataCenter defaultCenter] appLaunched];
    [self initSDK:launchOptions];
    [self initViews];
    return YES;
}

- (void)initSDK:(NSDictionary*)launchOptions
{
    MFWClientConfig *sdkConfig = [[MFWClientConfig alloc] init];
    sdkConfig.consumerKey = @"0";
    sdkConfig.consumerSecret = @"0";
    sdkConfig.apiHost = kAPI_HOST(@"");
    sdkConfig.defaultToken = @"0";
    sdkConfig.defaultTokenSecret = @"0";
    
    sdkConfig.appId = [AppDelegate appstoreId];
    
//    sdkConfig.ver = Interface_Ver;
//    sdkConfig.idfa = [self idfaString];
    
    //    sdkConfig.channelId = MFWSDK_Channel_AppStore;
    // read channel id from info.plist
    NSString *channelId = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"Channel_id"];
    sdkConfig.channelId = channelId;
    
#ifdef DEBUG
    sdkConfig.isDEBUG = YES;
#else
    sdkConfig.isDEBUG = NO;
#endif
//    [MFWHttpEngine setMFWHttpRequestFilter:globalRequestFilter];
    [MFWSdk appDidLaunched:launchOptions withConfiguration:sdkConfig];
    //    [UMOpus setAudioEnable:YES];
    
    
//    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//    center.delegate = self;
//    UNAuthorizationOptions types10 = UNAuthorizationOptionBadge|  UNAuthorizationOptionAlert | UNAuthorizationOptionSound;
//    [center requestAuthorizationWithOptions:types10     completionHandler:^(BOOL granted, NSError * _Nullable error) {
//        if (granted) {
//            //点击允许
//            //这里可以添加一些自己的逻辑
//        } else {
//            //点击不允许
//            //这里可以添加一些自己的逻辑
//        }
//    }];
//    
    //register remoteNotification types （iOS 8.0及其以上版本）
    //    UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
    //    action1.identifier = @"action1_identifier";
    //    action1.title=@"Accept";
    //    action1.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
    //
    //    UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];  //第二按钮
    //    action2.identifier = @"action2_identifier";
    //    action2.title=@"Reject";
    //    action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序，在后台处理
    //    action2.authenticationRequired = YES;//需要解锁才能处理，如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略；
    //    action2.destructive = YES;
    //
    //    UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
    //    categorys.identifier = @"category1";//这组动作的唯一标示
    //    [categorys setActions:@[action1,action2] forContext:(UIUserNotificationActionContextDefault)];
    //
    //    UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert
    //                                                                                 categories:[NSSet setWithObject:categorys]];
    //    [UMessage registerRemoteNotificationAndUserNotificationSettings:userSettings];
    
    
    //for log（optional）
    
//    NSString * appKey = @"55350d37e0f55a809300000f";
//    NSString * deviceName = [[[UIDevice currentDevice] name] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    NSString * mac = [self macString];
//    NSString * idfa = [self idfaString];
//    NSString * idfv = [self idfvString];
//    NSString * urlString = [NSString stringWithFormat:@"http://log.umtrack.com/ping/%@/?devicename=%@&mac=%@&idfa=%@&idfv=%@", appKey, deviceName, mac, idfa, idfv];
//    [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL: [NSURL URLWithString:urlString]] delegate:nil];
    
    //    [TalkingData openDebugLog:NO];
#ifdef DEBUG
    //    [TalkingData openDebugLog:YES];
    //        [MFWClick setLogEnabled:YES];
    //    [YWTest testLogin];
    
#endif

    
    
    [MFWAppVersionTracker appLaunched];
    
//    [Appirater appLaunched:NO];
//    [Appirater setAppId:[AppDelegate appstoreId]];
}

- (void)initViews{
    self.window = [[OIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
     MainTabBarViewController * tabbar = [[MainTabBarViewController alloc] init];
    self.mainPageController = tabbar;
    self.window.rootViewController = tabbar;
    self.tabbarHeight = tabbar.tabBar.frame.size.height;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
}

+ (NSString*)appstoreId;
{
    static NSString *__appstoreId = nil;
    if (!__appstoreId)
    {
        //__appstoreId = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"AppstoreID"];
//        __appstoreId = @"987444501";
    }
    
    return __appstoreId;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
